﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

namespace BookingEngine.Data
{
    public class HotelGetResvResponse
    {
        #region 基本属性

        private readonly XDocument _document;

        /// <summary>
        /// </summary>
        public bool IsSuccess
        {
            get
            {
                if (_document != null && _document.Root != null)
                {
                    var resultAttribute = _document.Root.Attribute("result");
                    if (resultAttribute != null)
                    {
                        return resultAttribute.Value.Equals("success", StringComparison.InvariantCultureIgnoreCase);
                    }
                }
                return false;
            }
        }

        /// <summary>
        /// </summary>
        public string ErrorMessage
        {
            get
            {
                if (_document != null && _document.Root != null)
                {
                    return _document.Root.Value;
                }
                return string.Empty;
            }
        }

        /// <summary>
        /// </summary>
        public string HotelCode
        {
            get
            {
                if (_document != null && _document.Root != null)
                {
                    var element = _document.Root.Descendants("prop").FirstOrDefault();
                    if (element != null)
                    {
                        return element.Value;
                    }
                }
                return string.Empty;
            }
        }

        /// <summary>
        /// </summary>
        public string HotelName
        {
            get
            {
                if (_document != null && _document.Root != null)
                {
                    var element = _document.Root.Descendants("propname").FirstOrDefault();
                    if (element != null)
                    {
                        return element.Value;
                    }
                }
                return string.Empty;
            }
        }

        /// <summary>
        /// </summary>
        public string Confnum
        {
            get
            {
                if (_document != null && _document.Root != null)
                {
                    var element = _document.Root.Descendants("confnum").FirstOrDefault();
                    if (element != null)
                    {
                        return element.Value;
                    }
                }
                return string.Empty;
            }
        }

        /// <summary>
        /// </summary>
        public string BookingStatus
        {
            get
            {
                if (_document != null && _document.Root != null)
                {
                    var element = _document.Root.Descendants("statusname").FirstOrDefault();
                    if (element != null)
                    {
                        return element.Value;
                    }
                }
                return string.Empty;
            }
        }

        /// <summary>
        /// </summary>
        public string SignStatus
        {
            get
            {
                if (_document != null && _document.Root != null)
                {
                    var element = _document.Root.Descendants("signname").FirstOrDefault();
                    if (element != null)
                    {
                        return element.Value;
                    }
                }
                return string.Empty;
            }
        }

        /// <summary>
        /// </summary>
        public DateTime CheckInDate
        {
            get
            {
                if (_document != null && _document.Root != null)
                {
                    var element = _document.Root.Descendants("indate").FirstOrDefault();
                    if (element != null)
                    {
                        return DateTime.Parse(element.Value);
                    }
                }
                return DateTime.Now;
            }
        }

        /// <summary>
        /// </summary>
        public DateTime CheckOutDate
        {
            get
            {
                if (_document != null && _document.Root != null)
                {
                    var element = _document.Root.Descendants("outdate").FirstOrDefault();
                    if (element != null)
                    {
                        return DateTime.Parse(element.Value);
                    }
                }
                return DateTime.Now;
            }
        }

        /// <summary>
        /// </summary>
        public DateTime BookDate
        {
            get
            {
                if (_document != null && _document.Root != null)
                {
                    var element = _document.Root.Descendants("bookdate").FirstOrDefault();
                    if (element != null)
                    {
                        return DateTime.Parse(element.Value);
                    }
                }
                return DateTime.Now;
            }
        }

        /// <summary>
        /// </summary>
        public string FirstName
        {
            get
            {
                if (_document != null && _document.Root != null)
                {
                    var element = _document.Root.Descendants("firstname").FirstOrDefault();
                    if (element != null)
                    {
                        return element.Value;
                    }
                }
                return string.Empty;
            }
        }

        /// <summary>
        /// </summary>
        public string LastName
        {
            get
            {
                if (_document != null && _document.Root != null)
                {
                    var element = _document.Root.Descendants("lastname").FirstOrDefault();
                    if (element != null)
                    {
                        return element.Value;
                    }
                }
                return string.Empty;
            }
        }

        /// <summary>
        /// </summary>
        public string Mobile
        {
            get
            {
                if (_document != null && _document.Root != null)
                {
                    var element = _document.Root.Descendants("mobile").FirstOrDefault();
                    if (element != null)
                    {
                        return element.Value;
                    }
                }
                return string.Empty;
            }
        }

        /// <summary>
        /// </summary>
        public string Phone
        {
            get
            {
                if (_document != null && _document.Root != null)
                {
                    var element = _document.Root.Descendants("phone").FirstOrDefault();
                    if (element != null)
                    {
                        return element.Value;
                    }
                }
                return string.Empty;
            }
        }

        /// <summary>
        /// </summary>
        public string Email
        {
            get
            {
                if (_document != null && _document.Root != null)
                {
                    var element = _document.Root.Descendants("email").FirstOrDefault();
                    if (element != null)
                    {
                        return element.Value;
                    }
                }
                return string.Empty;
            }
        }

        /// <summary>
        /// </summary>
        public string RoomType
        {
            get
            {
                if (_document != null && _document.Root != null)
                {
                    var element = _document.Root.Descendants("roomtypename").FirstOrDefault();
                    if (element != null)
                    {
                        return element.Value;
                    }
                }
                return string.Empty;
            }
        }

        /// <summary>
        /// </summary>
        public string RatePlan
        {
            get
            {
                if (_document != null && _document.Root != null)
                {
                    var element = _document.Root.Descendants("rateplanname").FirstOrDefault();
                    if (element != null)
                    {
                        return element.Value;
                    }
                }
                return string.Empty;
            }
        }

        /// <summary>
        /// </summary>
        public int Adults
        {
            get
            {
                if (_document != null && _document.Root != null)
                {
                    var element = _document.Root.Descendants("adults").FirstOrDefault();
                    if (element != null)
                    {
                        return int.Parse(element.Value);
                    }
                }
                return 0;
            }
        }

        /// <summary>
        /// </summary>
        public int Children
        {
            get
            {
                if (_document != null && _document.Root != null)
                {
                    var element = _document.Root.Descendants("children").FirstOrDefault();
                    if (element != null)
                    {
                        return int.Parse(element.Value);
                    }
                }
                return 0;
            }
        }

        /// <summary>
        /// </summary>
        public int Nights
        {
            get
            {
                if (_document != null && _document.Root != null)
                {
                    var element = _document.Root.Descendants("nights").FirstOrDefault();
                    if (element != null)
                    {
                        return int.Parse(element.Value);
                    }
                }
                return 0;
            }
        }

        /// <summary>
        /// </summary>
        public int Rooms
        {
            get
            {
                if (_document != null && _document.Root != null)
                {
                    var element = _document.Root.Descendants("rooms").FirstOrDefault();
                    if (element != null)
                    {
                        return int.Parse(element.Value);
                    }
                }
                return 0;
            }
        }

        /// <summary>
        /// </summary>
        public decimal TotalPrice
        {
            get
            {
                if (_document != null && _document.Root != null)
                {
                    var element = _document.Root.Descendants("totalrevenue").FirstOrDefault();
                    if (element != null)
                    {
                        return decimal.Parse(element.Value);
                    }
                }
                return 0;
            }
        }

        /// <summary>
        /// </summary>
        public List<Tuple<DateTime, decimal, decimal>> Rates
        {
            get
            {
                var rates = new List<Tuple<DateTime, decimal, decimal>>();
                if (_document != null && _document.Root != null)
                {
                    var elements = _document.Root.Descendants("bookedrate").ToList();
                    foreach (var element in elements)
                    {
                        var date = DateTime.Now;
                        var dateElement = element.Descendants("date").FirstOrDefault();
                        if (dateElement != null)
                        {
                            date = DateTime.Parse(dateElement.Value);
                        }
                        decimal rate = 0;
                        var rateElement = element.Descendants("rate").FirstOrDefault();
                        if (rateElement != null)
                        {
                            rate = decimal.Parse(rateElement.Value);
                        }
                        decimal extracharge = 0;
                        var extrachargeElement = element.Descendants("extracharge").FirstOrDefault();
                        if (extrachargeElement != null)
                        {
                            extracharge = decimal.Parse(extrachargeElement.Value);
                        }
                        rates.Add(new Tuple<DateTime, decimal, decimal>(date, rate, extracharge));
                    }
                }
                return rates;
            }
        }

        /// <summary>
        /// </summary>
        public string GuaranteeRule
        {
            get
            {
                if (_document != null && _document.Root != null)
                {
                    var element = _document.Root.Descendants("guarruledetail").FirstOrDefault();
                    if (element != null)
                    {
                        var descElement = element.Descendants("description").FirstOrDefault();
                        if (descElement != null)
                        {
                            return descElement.Value;
                        }
                    }
                }
                return string.Empty;
            }
        }

        /// <summary>
        /// </summary>
        public string CancellationRule
        {
            get
            {
                if (_document != null && _document.Root != null)
                {
                    var element = _document.Root.Descendants("cxlruledetail").FirstOrDefault();
                    if (element != null)
                    {
                        var descElement = element.Descendants("description").FirstOrDefault();
                        if (descElement != null)
                        {
                            return descElement.Value;
                        }
                    }
                }
                return string.Empty;
            }
        }

        #endregion

        #region 构造函数

        /// <summary>
        /// </summary>
        /// <param name="responseXml"> </param>
        public HotelGetResvResponse(string responseXml)
        {
            _document = XDocument.Parse(responseXml);
        }

        #endregion
    }
}